<template>
  <div class="box">
    <div class="top">
        <div>
            <template>
                <el-radio v-model="radio" label="1" @input="handleChange">按时间排序</el-radio>
                <el-radio v-model="radio" label="2" @input="handleChange">按点赞排序</el-radio>
            </template>
        </div>
        <div class="search">
            <el-input v-model="params.title" placeholder="请输入内容"></el-input>
            <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        </div>
    </div>
    <div class="bottom">
        <el-table
            :data="tableData"
            stripe
            style="width: 100%">
            <el-table-column
            prop="title"
            label="题目"
            width="180">
            </el-table-column>
            <el-table-column
            prop="blogtime"
            label="发布时间"
            >
            </el-table-column>
            <el-table-column
            prop="agree"
            label="已获赞"
            width="180">
            </el-table-column>
            <el-table-column
            prop="count"
            label="评论条数"
            width="180">
            </el-table-column>
            <el-table-column
            fixed="right"
            label="操作"
            width="180">
            <template slot-scope="scope">
                <!-- <el-button type="text" size="small" @click="handleClick(scope.row)">
                    删除
                </el-button> -->
                <el-popconfirm
                title="确定删除吗？"
                @confirm="handleClick(scope.row)"
                >
                    <el-button slot="reference">删除</el-button>
                </el-popconfirm>
            </template>
            </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <div class="block">
        <el-pagination
            layout="prev, pager, next"
            :current-page="pageInfo.pageNum"
            @current-change="handleCurrentChange"
            :page-size="pageInfo.pageSize"
            :hide-on-single-page="true"
            :total="pageInfo.total"
        >
        </el-pagination>
        </div>
    </div>
  </div>
</template>

<script>
import {BlogGetAgree,BlogGetTime,BlogGetSearch,BlogDelete} from '@/request/api'
export default {
  name: 'BlogAdmin',
  data(){
    return{
      input:'',
      radio:"1",
      tableData:[],
      pageInfo:{},
      params:{
        pageNum:1,
        title:''
      },
      delParams:{
        id:null
      }
    }
  },
  methods:{
    async handleCurrentChange(index){
        this.params.pageNum = index
        let res = await BlogGetAgree(this.params)
        this.pageInfo = res.data.data
        this.tableData = res.data.data.list
    },
    async handleChange(label){
        this.params.pageNum = 1
        if(label == 1){
            let res = await BlogGetAgree(this.params)
            this.pageInfo = res.data.data
            this.tableData = res.data.data.list
        }else{
            let res = await BlogGetTime(this.params)
            this.pageInfo = res.data.data
            this.tableData = res.data.data.list
        }
    },
    async handleSearch(){
        this.params.pageNum = 1
        let res =await BlogGetSearch(this.params)
        this.pageInfo = res.data.data
        this.tableData = res.data.data.list
    },
    async handleClick(row){
        this.delParams.id = row.postid
        let res = await BlogDelete(this.delParams)
        if(res.data.state == 200){
            // 刷新数据
            this.params.pageNum = 1
            let res = await BlogGetAgree(this.params)
            this.pageInfo = res.data.data
            this.tableData = res.data.data.list
        }
        console.log("res",res)
    }
  },
  async created(){
    let res = await BlogGetAgree(this.params)
    this.pageInfo = res.data.data
    this.tableData = res.data.data.list
  }
}
</script>

<style scoped>
.box{
    padding: 10px;
    width: 100%;
}
.top{
    display: flex;
    flex-direction: row;
    justify-content: flex-end;
    height: 40px;
    line-height: 40px;
    width: 100%;
}
.search{
    display: flex;
    flex-direction: row;
    margin-right: 10px;
    margin-left: 10px;
}
.block{
    text-align: center;
    margin-top: 20px;   
}
</style>
